<template>
	<a-card :bordered="false">
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="openDetailForm(1)" v-if="hasPerm('sysHolidayAccountAdd')">
						<template #icon><plus-outlined /></template>
						添加余额
					</a-button>
					<a-button type="danger" @click="openDetailForm(2)" v-if="hasPerm('sysHolidayAccountAdd')">
						<template #icon><minus-outlined /></template>
						扣减余额
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'createTime'">
					{{ moment(record.createTime).format('YYYY-MM-DD') }}
				</template>
				<template v-if="column.dataIndex === 'reduce'">
					<span :style="record.reduce == 1 ? 'color:blue' : 'color:red'">{{
						record.reduce == 1 ? '+' + record.addReduce : '-' + record.addReduce
					}}</span>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('sysHolidayAccountEdit')">撤销</a>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<detail-form ref="detailFromRef" @successful="table.refresh(true)" />
</template>

<script setup name="holidayaccount">
	import Form from './form.vue'
	import sysHolidayAccountDetailedApi from '@/api/biz/sysHolidayAccountDetailedApi'
	import userApi from '@/api/sys/userApi'
	import moment from 'moment'
	import detailForm from './detailForm.vue'
	let searchFormState = reactive({})
	const table = ref()
	const treeDefaultExpandedKeys = ref([])
	const treeData = ref([])
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const detailFromRef = ref()
	const columns = [
		{
			title: '发生日期',
			dataIndex: 'createTime',
			align: 'center',
			width: '120px'
		},
		{
			title: '增减',
			dataIndex: 'reduce',
			align: 'center',
			width: '80px'
		},
		{
			title: '明细',
			align: 'left',
			dataIndex: 'reason'
		}
	]
	const props = defineProps({
		holidayId: {
			type: String,
			default: ''
		},
		leaveType: {
			type: Number,
			default: 1
		}
	})
	// 操作栏通过权限判断是否显示
	if (hasPerm(['sysHolidayAccountEdit', 'sysHolidayAccountDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '80px'
		})
	}
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		searchFormParam.holidayId = props.holidayId
		searchFormParam.leaveType = props.leaveType
		return sysHolidayAccountDetailedApi
			.sysHolidayAccountDetailedPage(Object.assign(parameter, searchFormParam))
			.then((data) => {
				return data
			})
	}
	const openDetailForm = (reduce) => {
		const record = {
			holidayId: props.holidayId,
			leaveType: props.leaveType,
			reduce: reduce
		}
		detailFromRef.value.onOpen(record)
	}
</script>
<style lang="less" scoped>
	:deep(.ant-card-body) {
		padding: 0px;
	}
</style>
